#ifndef __DEV_H__
#define __DEV_H__

#include "typedef.h"

#define DEV_MAX_BRM_BMS_CONNECT_VERSION_LEN		(3u)
#define DEV_MAX_BRM_VIN_LEN				        (17u)
#define DEV_MAX_BRM_BMS_SOFTWARE_VERSION_LEN	(8u)
#define DEV_CHARGE_GUN_CNT                       (2u)       //充电桩枪数量，实际个数要看配置数据

enum SYS_STATE{
	SYS_STATE_INIT = 0,
	SYS_STATE_NORMAL,
	SYS_STATE_ALARM,
	SYS_STATE_ADMIN,
};

enum DevMode{
    DEV_CHARGE_MODE_AC = 0,
    DEV_CHARGE_MODE_DC,
};

enum GunConnectState{
  GUN_UNCONNECT_CAR = 0,
  GUN_CONNECT_CAR,  
};

/*充电枪实时信息*/
struct ClientUserGunInfo{
  int32_t car_connection_stat;/*车连接状态  0未连接 2已连接*/
	int32_t relay_state;//输出继电器状态 1:闭合 0:打开
	int32_t soc_percent; /*当前电量*/
	int32_t dc_charge_voltage; /*直流充电电压*/
	int32_t dc_charge_current; /*直流充电电流*/
	int32_t bms_need_voltage; /*BMS需求电压*/
	int32_t bms_need_current; /*BMS需求电流*/
  int32_t bms_charge_mode;  /* BMS充电模式 */
	int32_t ac_a_vol; /*交流A相充电电压*/
	int32_t ac_b_vol; /*交流B相充电电压*/
	int32_t ac_c_vol; /*交流C相充电电压*/
	int32_t ac_a_cur; /*交流A相充电电流*/
	int32_t ac_b_cur; /*交流B相充电电流*/
	int32_t ac_c_cur; /*交流C相充电电流*/
	int32_t meter_kwh_num; /*当前电表读数 */
	int32_t charge_power_kw; /*充电功率 */	
    int32_t left_time;  /*充满剩余时间*/
};

/*BMS实时信息*/
struct  ClientUserBmsInfo{	
	char_t brm_bms_connect_version[DEV_MAX_BRM_BMS_CONNECT_VERSION_LEN+1]; /*BRM-BMS通讯协议版本号*/
	int32_t brm_battery_type;/*电池类型*/
	int32_t brm_battery_power;
	int32_t brm_battery_volt;/* 整车动力蓄电池系统额定总电压*/
	int32_t brm_battery_supplier; /* 电池生产厂商*/
	int32_t brm_battery_seq;/* 电池组序号*/
	int32_t brm_battery_produce_year;/* 电池组生厂日期：年*/
	int32_t brm_battery_produce_month;/* 电池组生厂日期：月*/
	int32_t brm_battery_produce_day;/* 电池组生厂日期：日*/
	int32_t brm_battery_charge_count;/* 电池组充电次数*/
	int32_t brm_battery_property_identification; /* 电池组产权标识*/
	char_t brm_vin[DEV_MAX_BRM_VIN_LEN+1];/* 车辆识别码vin */
	char_t brm_bms_software_version[DEV_MAX_BRM_BMS_SOFTWARE_VERSION_LEN+1];/* BMS软件版本号*/
	int32_t bcp_max_voltage;/* 单体动力蓄电池最高允许充电电压*/
	int32_t bcp_max_current;/* 最高允许充电电流*/
	int32_t bcp_max_power;/* 动力蓄电池标称总能量*/
	int32_t bcp_total_voltage;/* 最高允许充电总电压*/
	int32_t bcp_max_temperature;/* 最高允许温度 */
	int32_t bcp_battery_soc;/* 整车动力蓄电池荷电状态*/
	int32_t bcp_battery_soc_current_voltage; /* 整车动力蓄电池当前电池电压*/
	int32_t bro_bms_is_ready; /* 是否充电准备好*/
	int32_t bcl_voltage_need; /* 电压需求*/
	int32_t bcl_current_need;/* 电流需求*/
	int32_t bcl_charge_mode;/* 充电模式*/
	int32_t bcs_test_voltage;/* 充电电压测量值 */
	int32_t bcs_test_current;/* 充电电流测量值*/
	int32_t bcs_max_single_voltage; /* 最高单体动力蓄电池电压*/
	int32_t bcs_max_single_no;/* 最高单体动力蓄电池组号*/
	int32_t bcs_current_soc;/* 当前荷电状态soc%*/
	int32_t charge_time_left;/* 估算剩余充电时间*/
	int32_t bsm_single_no;/* 最高单体动力蓄电池电压所在编号*/
	int32_t bsm_max_temperature; /* 最高动力蓄电池温度*/
	int32_t bsm_max_temperature_check_no;/* 最高温度检测点编号*/
	int32_t bsm_min_temperature;/* 最低动力蓄电池温度*/
	int32_t bsm_min_temperature_check_no;/* 最低动力蓄电池温度检测点编号*/
	int32_t bsm_voltage_too_high_or_too_low;/* 单体动力蓄电池电压过高或过低*/
	int32_t bsm_car_battery_soc_too_high_or_too_low;/* 整车动力蓄电池荷电状态soc过高或过低*/
	int32_t bsm_car_battery_charge_over_current;/* 动力蓄电池充电过电流*/
	int32_t bsm_battery_temperature_too_high; /* 动力蓄电池温度过高 */
	int32_t bsm_battery_insulation_state;/* 动力蓄电池绝缘状态*/
	int32_t bsm_battery_connect_state;/* 动力蓄电池组输出连接器连接状态 */
	int32_t bsm_allow_charge;/* 允许充电*/
	int32_t bst_bms_soc_target;/* BMS达到所需求的SOC目标值*/
	int32_t bst_bms_voltage_target;/* BMS达到总电压的设定值*/
	int32_t bst_single_voltage_target; /* 达到单体电压的设定值 */
	int32_t bst_finish;/* 充电桩主动终止*/
	int32_t bst_isolation_error;/* 绝缘故障*/
	int32_t bst_connect_over_temperature;/* 输出连接器过温故障*/
	int32_t bst_over_temperature; /* BST-BMS元件，输出连接器过温 */
	int32_t bst_connect_error; /* BST-充电连接器故障*/
	int32_t bst_battery_over_temperature;/* BST-电池组温度过高故障*/
	int32_t bst_high_voltage_relay_error;/* 高压继电器故障*/
	int32_t bst_point2_test_error;/* 检测点2电压检测故障*/
	int32_t bst_other_error;/* 其他故障*/
	int32_t bst_current_too_high;/* 电流过大*/
	int32_t bst_voltage_too_high;/* 电压异常 */
	int32_t bst_stop_soc;/* 终止荷电状态soc */
	int32_t bsd_battery_low_voltage;/* BSD-动力蓄电池单体最低电压*/
	int32_t bsd_battery_high_voltage;/* BSD-动力蓄电池单体最高电压 */
	int32_t bsd_battery_low_temperature;/* BSD-动力蓄电池最低温度*/
	int32_t bsd_battery_high_temperature;/* BSD-动力蓄电池最高温度*/
	int32_t bem_error_spn2560_00; /* BEM-接收SPN2560=0x00的充电桩辨识报文超时*/
	int32_t bem_error_spn2560_aa;/* BEM-接收SPN2560=0xaa的充电桩辨识报文超时*/
	int32_t bem_error_time_sync;  /* BEM-接收充电桩的时间同步和最大输出能力报文超时*/
	int32_t bem_error_ready_to_charge;/* BEM-接收充电桩完成充电准备报文超时*/
	int32_t bem_error_receive_status;/* BEM-接收充电桩充电状态报文超时*/
	int32_t bem_error_receive_stop_charge;/* BEM-接收充电桩终止充电报文超时*/ 
	int32_t bem_error_receive_report; /* BEM-接收充电桩充*/
	int32_t bem_error_other;/* BEM-其他*/
};

struct ClientCallback{
    int32_t (*charge_idle)(uint32_t gun_id);
    int32_t (*charge_stoped)(uint32_t gun_id, const char_t *stop_reason);
    int32_t (*error_occured)(uint32_t gun_id, const char_t *err_no);
    int32_t (*error_recovered)(uint32_t gun_id, const char_t *err_no);
    int32_t (*card_swip)(uint32_t gun_id, char_t *id, char_t *pwd, uint8_t type, uint32_t value);
    int32_t (*state_reup)(uint32_t gun_id);
};

void dev_set_client_callback(const struct ClientCallback *callback);

struct ClientUserGunInfo *dev_get_user_client_gun_data(void);
struct  ClientUserBmsInfo *dev_get_user_client_bms_data(void);


int32_t dev_isswipcard(char_t *cardid);
int32_t dev_is_can_start_charge(int32_t gunid, char_t *code);

enum SYS_STATE dev_state(void);
enum MODULE_STATE dev_module_state(int32_t gunid);

int32_t dev_setsystime(void *p);
int32_t dev_setqrcode(int32_t gunid, char_t *qrcode);
int32_t dev_isallowcharge(int32_t gunid);
int32_t dev_start_charge_with_check(int32_t gunid, char_t *code);
int32_t dev_startcharge(int32_t gunindex);
int32_t dev_swipcard_select(int32_t gunid);
int32_t dev_stopcharge_server(int32_t gunindex);
int32_t dev_stopcharge_user(int32_t gunindex);
int32_t dev_reboot(void);
void dev_net_connect_state(int32_t state);
void dev_update_charge_total(int32_t gunindex, int32_t charge);
int32_t dev_swipcard_select(int32_t gunid);
void dev_gui_return(int32_t gunid);
int dev_get_dev_mode(void);
int32_t dev_is_use_net(void);


int32_t dev_init(void);
int32_t dev_is_init_succ(uint8_t gun_id);
int32_t dev_idle_start(uint8_t gun_id);

void dev_enable_swipcard(void);
void dev_disable_swipcard(void);

//返回 < 0 启动失败 = 0 正在启动中 > 0 启动成功
int32_t dev_is_start_charging_finish(uint8_t gun_id, char_t *reasion);
int32_t dev_get_meter_kwh(uint8_t gun_id);
int32_t dev_get_vol(uint8_t gun_id);
int32_t dev_get_current(uint8_t gun_id);
int32_t dev_get_need_vol(uint8_t gun_id);
int32_t dev_get_need_currt(uint8_t gun_id);
int32_t dev_get_left_time(uint8_t gun_id);
int32_t dev_get_soc(uint8_t gun_id);

int32_t dev_is_stop_charge(uint8_t gun_id, char_t *reasion);
int32_t dev_is_stop_charge_finish(uint8_t gun_id);
int32_t dev_is_low_current(uint8_t gun_id);
int32_t dev_start_charge_fail(uint8_t gun_id, char_t *reasion);
int32_t dev_stop_charge(uint8_t gun_id, char_t *reasion);
int32_t dev_stop_charge_up_client(uint8_t gun_id, const char_t *reasion);
int32_t dev_get_net_state(void);
int32_t dev_card_swip(uint8_t gun_id, char_t *card_id, char_t *pwd, uint32_t type, uint32_t value);

int32_t dev_rfcard_read(char_t *card_id);
void dev_adv_light_mgr(void);
int32_t dev_state_change_up_client(uint8_t gun_id);
int32_t dev_state_charge_idle(uint8_t gun_id);
int32_t dev_sync_time(uint64_t sec);
int32_t dev_sync_datetime(int year, uint8_t mon, uint8_t day, uint8_t hour, uint8_t min, uint8_t sec,uint8_t week);
void dev_set_net_state(uint8_t state);
void dev_poll(void);
int32_t dev_alarm_init(uint8_t gun_id);

//接口函数, ac,dc 实现不一样
uint8_t dev_get_gun_link(uint8_t gun_id); 
uint8_t dev_get_relay_state(uint8_t gun_id);
int32_t dev_init_init(uint8_t gun_id);
int32_t dev_idle_init(uint8_t gun_id);

void controler_keeplive(uint8_t gun_id);
void controler_set_time(void);
int32_t controler_init(void);

uint32_t controler_get_vol(uint8_t gun_id);
uint32_t controler_get_current(uint8_t gun_id);
uint32_t controler_get_need_vol(uint8_t gun_id);
uint32_t controler_get_need_currt(uint8_t gun_id);
uint32_t controler_get_left_time(uint8_t gun_id);
uint32_t controler_get_soc(uint8_t gun_id);

#endif

